package com.lxyk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lxyk.yygh.cmn.client.DictFeign;
import com.lxyk.yygh.hosp.repository.DepartmentRepository;
import com.lxyk.yygh.hosp.repository.HospitalRepository;
import com.lxyk.yygh.hosp.service.DepartmentService;
import com.lxyk.yygh.hosp.service.HospitalService;
import com.lxyk.yygh.model.hosp.BookingRule;
import com.lxyk.yygh.model.hosp.Hospital;
import com.lxyk.yygh.vo.hosp.DepartmentVo;
import com.lxyk.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import static com.lxyk.yygh.enums.DictEnum.HOSTYPE;

/**
 * mongodb数据库
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeign dictFeign;

    /**
     * 上传医院信息
     * @param paramsMap
     */
    @Override
    public void save(Map<String, Object> paramsMap) {
      //把map转成对象Hospital
        String mapString = JSONObject.toJSONString(paramsMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
      // 1.是否存在hospital,若存在，则修改
        String hoscode = hospital.getHoscode();
      //操作mongodb数据库进行查询
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospitalExist!=null){
            //进行修改
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setIsDeleted(0);
            hospitalRepository.save(hospitalExist);

        }
        else{
            //添加
            //0 没上线 1 上线
            hospital.setStatus(0);
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            System.out.println("时间"+new Date());
            hospitalRepository.insert(hospital);
        }
    }

    /**
     * 查询医院接口
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospitalByHoscode;
    }

    /**
     * 医院的分页查询
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> listHosp(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
       //创建pageable对象,page从0开始
        PageRequest pageable = PageRequest.of(page - 1, limit);
        //创建匹配器，使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改成模糊查询
                .withIgnoreCase(true);//忽略大小写

        Hospital hospital = new Hospital();
        //HospitalVo转换为hospital
        if(hospitalQueryVo!=null){
            BeanUtils.copyProperties(hospitalQueryVo,hospital);
        }
        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        // TODO: 2023/7/20  远程调用cmn模块数据字典，省市区
        pages.stream().forEach(hosp->{
            this.packHospital(hosp);//整个结束pages也会改变结果
        });
//
        return pages;
    }


    public Hospital packHospital(Hospital hospital){
         //远程调用
        //hospital.getHostype()--->得到1  得到的是字典中的value值
        String hospType = dictFeign.getName(HOSTYPE.getDictCode(), hospital.getHostype());
        //获取省市区
        String provinceName = dictFeign.getName(hospital.getProvinceCode());
        String cityName = dictFeign.getName(hospital.getCityCode());
        String districtName = dictFeign.getName(hospital.getDistrictCode());
        //设置
        hospital.getParam().put("hospType",hospType);
        hospital.getParam().put("fullAddress",provinceName+cityName+districtName);

        return hospital;

    }

//    更新医院上线状态
//    状态 0：未上线 1：已上线
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id进行查找
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());

        hospitalRepository.save(hospital);
    }

    //医院详情
    @Override
    public Map<String, Object> getHospById(String id) {

        Hospital hospital = hospitalRepository.findById(id).get();
        HashMap<String, Object> map = new HashMap<>();
        //医院的基本信息（包含医院等级
        map.put("hospital",hospital);
        //使用map
        //hospital里面有BookingRule对象
        //	private BookingRule bookingRule; BookingRule的属性还有list
        map.put("bookingRule",hospital.getBookingRule());//
        //不需要重新返回?
        hospital.setBookingRule(null);
        return map;
    }

//    //根据hoscode获取医院名字
    @Override
    public String getHospName(String hoscode) {
        //查询
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";
    }

    /**********前台业务了************/
    //根据医院名字模糊查询
    @Override
    public List<Hospital> findByHosName(String hosname) {
        List<Hospital> hospitalList = hospitalRepository.findHospitalByHosnameLike(hosname);
        return hospitalList;
    }

    /**
     * 医院预约挂号详情
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> hospitalDetail(String hoscode) {
        //根据医院hoscode查询医院信息
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        BookingRule bookingRule = hospital.getBookingRule();

        Map<String,Object> map=new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

}
